<?php
	
	namespace Aspic\Form\Element;
	use Aspic\Security\Security as S;

	/**
	* Standard form element with a label
	*/
	abstract class AbstractStandardElement extends AbstractValidatableElement {
		protected $_labelText;
		protected $_labelClass;
		protected $_labelAndElementSeparator;
		
		/**
		* @param string name The name of the element that will be retrieve by php for this element (in $_POST)
		*/
		public function __construct($name, $label = '', array $options = array(), array $attributes = array(), array $validations = array()) {			
			parent::__construct($name, $options, $attributes, $validations);
			
			$this->opt('labelText', $label, true);
			$this->opt('labelClass', '', true);
			$this->opt('labelAndElementSeparator', '', true);
			
			if(!isset($attributes['id'])) { // To avoid earasing user defined id in attributes array
				$this->attr('id', $this->getDefaultId());
			}
			
		}
		
		public function outElement() {
			return 
			$this->outLabel().
			$this->opt('labelAndElementSeparator').
			$this->outInput();
		}
		
		public function outLabel() {
			$out = '<label for="'.$this->attr('id').'" class="'.$this->opt('labelClass').'">'.S::escapeXss($this->opt('labelText')).'</label>';
			
			return $out;
		}
		
		/**
		* The html output for the element
		*/
		abstract public function outInput();
		
		/**
		* Used to force child classes to define a default id   
		* because it is used for the 'for' attribute of the label
		*/
		abstract protected function getDefaultId();

	}
	
?>